package com.yunlin.blecontroler;

import com.yunlin.bean.OpenLockStatus;
import com.yunlin.event.ReadDeviceInfoEvent;
import com.yunlin.protocal.BteErrorData;
import com.yunlin.protocal.BteFunctionData;
import com.yunlin.utils.FormatUtils;

/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ 
       Author   :  lixiaodaoaaa
       Date     :  2018/7/9
       Time     :  14:30
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public class ReadDataUtils{
    
    /**
     * @param readData
     * @return 判断发送的数据是不是数据格式错误（如果是返回True) aaeeeeeeee (read）代表数据格式出错，返回True
     */
    public static boolean checkSendDataFormatterIsError(String readData){
        if (readData.length() > 4) {
            String functionData=readData.substring(2, 4);
            if (functionData.toLowerCase().equals(BteErrorData.DATA_ERROR)) {
                return true;
            }
        }
        return false;
    }
    
    
    public static boolean isNeedOpenLock(String rawData){
        String dataEnd=rawData.substring(rawData.length() - 2, rawData.length());
        return !dataEnd.equals(BteFunctionData.RESPONSE_OPENLOCK);
    }
    
    public static boolean isReadDeviceInfo(String rawData){
//        04e0020202020659eb179a77515b674b
        if (rawData.length() < 4) {
            return false;
        }
        if (rawData.substring(0, 2).equals(BteFunctionData.READ_DEVICE_INFO)) {
            return true;
        }
        return false;
    }
    
    public static String readBatteryInfo(String rawaData){
        //04e0020202020659eb179a77515b674b
        if (rawaData.length() >= 16) {
            String batteryHexStr=rawaData.substring(14, 16);
            return FormatUtils.hexString2IntValue(batteryHexStr) + "%";
        }
        return "";
    }
    
    public static ReadDeviceInfoEvent readDeviceInfo(String rawaData){
        //04 e0 02  02 02 02 06 59 eb17 9a77515b674b
        String voiceStr="";
        String batteryStr="";
        String voltageStr="";
        
        if (rawaData.length() >= 16) {
            String voiceHexStr=rawaData.substring(12, 14);
            String batteryHexStr=rawaData.substring(14, 16);
            String voltageHexStr=rawaData.substring(16, 20);
            voiceStr=FormatUtils.hexString2IntValue(voiceHexStr) * 10 + "%";
            batteryStr=FormatUtils.hexString2IntValue(batteryHexStr) + "%";
            int voltageAll=FormatUtils.hexString2IntValue(voltageHexStr);
            voltageStr=Float.valueOf(voltageAll) / 10000 + "" + "V";
        }
        return new ReadDeviceInfoEvent(voiceStr, batteryStr, voltageStr);
    }
    
    public static OpenLockStatus readDataToOpenLockStatus(String rawData, int useTime){
        String dataEnd=rawData.substring(rawData.length() - 2, rawData.length());
        if (!dataEnd.equals(BteFunctionData.RESPONSE_OPENLOCK)) {
            return null;
        }
        //BB4F4B 开锁指令
        if (rawData.toLowerCase().contains("4f4b")) {
            return new OpenLockStatus(true, useTime);
        }else {
            return new OpenLockStatus(false, useTime);
        }
    }
}
